﻿CREATE PROCEDURE [dbo].[yaf_watchforum_list](
                @UserID INT)
AS
    BEGIN
        SELECT a.*,
               ForumName = b.Name,
               Messages = (SELECT COUNT(1)
                           FROM   yaf_Topic x,
                                  yaf_Message y
                           WHERE  x.ForumID = a.ForumID
                           AND y.TopicID = x.TopicID),
               Topics = (SELECT COUNT(1)
                         FROM   yaf_Topic x
                         WHERE  x.ForumID = a.ForumID
                         AND x.TopicMovedID IS NULL),
               b.LastPosted,
               b.LastMessageID,
               LastTopicID = (SELECT TopicID
                              FROM   yaf_Message x
                              WHERE  x.MessageID = b.LastMessageID),
               b.LastUserID,
               LastUserName = Isnull(b.LastUserName,(SELECT Name
                                                     FROM   yaf_User x
                                                     WHERE  x.UserID = b.LastUserID))
        FROM   yaf_WatchForum a,
               yaf_Forum b
        WHERE  a.UserID = @UserID
        AND b.ForumID = a.ForumID
    END


